<template>
  <el-form
    ref="pwdRef"
    label-position="left"
    :model="user"
    :rules="rules"
    :label-width="useFormLabelWidth('180px', '80px')"
  >
    <el-form-item v-for="pwd of set_pwd" :key="pwd.value" :label="pwd.label" :prop="pwd.value">
      <el-input
        v-model="user[pwd.value]"
        :placeholder="`${$t('common.please_enter')}${pwd.label}`"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="submit">{{ $t('common.save') }}</el-button>
      <el-button type="danger" size="small" @click="close">{{ $t('common.close') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup name="setpassword">
import { set_pwd } from '@/views/Personal/_personal'
import { InternalRuleItem, Value } from 'async-validator'
import { FormRules } from 'element-plus'
import { useFormLabelWidth } from '@/hooks/useFormLabelWidth'
const user = reactive<{
  [key: string]: undefined | string // 字段扩展声明
  oldPassword: undefined | string
  newPassword: undefined | string
  confirmPassword: undefined | string
}>({
  oldPassword: undefined,
  newPassword: undefined,
  confirmPassword: undefined
})

const equalToPassword = (
  rule: InternalRuleItem,
  value: Value,
  // eslint-disable-next-line no-unused-vars
  callback: (error?: string | Error) => void
) => {
  if (user.newPassword !== value) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}
const rules: FormRules = {
  oldPassword: [{ required: true, message: '旧密码不能为空', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    { required: true, validator: equalToPassword, trigger: 'blur' }
  ]
}
const submit = () => {}
const close = () => {}
</script>

<style scoped lang="scss"></style>
